package com.siwei.mes.websocket.controller;

import com.siwei.mes.common.annotation.AnonymousAccess;
import com.siwei.mes.common.base.BaseParams;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

/**
 * @author chenmaolin
 * @version 1.0
 * @date 2023/4/13
 * @Description websocket日志接口
 */
@Controller
public class LogController {
    @Value("${spring.profiles.active}")
    private String active;

    @AnonymousAccess
    @RequestMapping("/logs")
    public String logs(Model model, HttpServletRequest request) {
        String socketUrl = this.getSocketUrl(request);
        String path = "/websocket/logging";
        switch (active) {
            case BaseParams.TEST:
                socketUrl = "ws://106.54.139.68:8091/mes-platform";
                break;
            case BaseParams.CUSTOMER:
                socketUrl = "ws://60.170.157.7:8092/mes-platform";
                break;
            case BaseParams.PROD:
                socketUrl = "ws://175.27.170.161:8091";
                break;
            default:
        }
        model.addAttribute("socketUrl", socketUrl + path);
        model.addAttribute("targetName", "蚌埠实时日志");
        return "/logging";
    }

    /**
     * 获取websocket地址
     * ws://106.54.139.68:8091/mes-platform/websocket/logging
     *
     * @param request
     * @return
     */
    public String getSocketUrl(HttpServletRequest request) {
        /**当前链接使用的协议*/
        return "ws" + "://" + request.getServerName()// 服务器地址
//                + "://" + serverName//服务器地址
                + ":" + request.getServerPort() // 端口号
                + request.getContextPath();
    }
}
